package cn.ffcs.uec.outStaff.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.common.util.NodeVoUtil;
import cn.ffcs.uec.common.util.UecConstants;
import cn.ffcs.uec.organization.manager.UecOrgDeptManager;
import cn.ffcs.uec.organization.manager.UecOrgOrgsManager;
import cn.ffcs.uec.outStaff.action.bean.UecOutStaffWorkEditBean;
import cn.ffcs.uec.outStaff.manager.UecOutStaffManager;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.outStaff.model.UecOutStaffWork;
import cn.ffcs.uec.post.manager.UecPostManager;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;
import cn.ffcs.uom.systemconfig.manager.IdentityCardConfigManager;

@Controller
@Scope("prototype")
public class UecOutStaffWorkEditComposer extends BasePortletComposer {
	private static final long serialVersionUID = -6096343177536382090L;

	private UecOutStaffWorkEditBean bean = new UecOutStaffWorkEditBean();

	private String opType;

	private UecOutStaffManager uecOutStaffManager = (UecOutStaffManager) ApplicationContextUtil
			.getBean("uecOutStaffManager");

	@Resource
	private IdentityCardConfigManager identityCardConfigManager;

	private UecOutStaffWork uecOutStaffWork;

	private UecOutStaffInfo uecOutStaffInfo;

	private UecOrgOrgsManager uecOrgOrgsManager = (UecOrgOrgsManager) ApplicationContextUtil
			.getBean("uecOrgOrgsManager");

	private UecOrgDeptManager uecOrgDeptManager = (UecOrgDeptManager) ApplicationContextUtil
			.getBean("uecOrgDeptManager");

	private UecPostManager uecPostManager = (UecPostManager) ApplicationContextUtil.getBean("uecPostManager");

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);

	}

	/**
	 * window初始化.
	 * 
	 * @throws Exception
	 *             异常
	 */
	public void onCreate$uecOutStaffWorkEditWin() throws Exception {
		bindCombobox();
		opType = (String) arg.get("opType");
		uecOutStaffInfo = (UecOutStaffInfo) arg.get("uecOutStaffInfo");
		if (opType.equals("add")) {
			bean.getUecOutStaffWorkEditWin().setTitle("外包人员工作记录新增");

		} else if (opType.equals("edit")) {

			bean.getUecOutStaffWorkEditWin().setTitle("外包人员工作记录修改");
			strToDate();
		} else if (opType.equals("view")) {
			bean.getUecOutStaffWorkEditWin().setTitle("外包人员工作记录查看");
			strToDate();
			this.codeTranslateDisplay();
			PubUtil.disableComponent(bean.getUecOutStaffWorkEditWin());
		}
	}

	/**
	 * 保存.
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = checkUacStaffWorkData();
		if (msg != null) {
			Messagebox.show(msg);
			return;
		}
		uecOutStaffWork = (UecOutStaffWork) arg.get("uecOutStaffWork");
		UecOutStaffWork uecOutStaffWorkObj = UecOutStaffWork.newInstance();
		if (uecOutStaffWork != null) {
			PubUtil.fillPoFromEditorBean(uecOutStaffWork, bean);
		} else {
			uecOutStaffWork = uecOutStaffWorkObj;
			uecOutStaffWork.setOutStaffId(uecOutStaffInfo.getId());
			PubUtil.fillPoFromEditorBean(uecOutStaffWork, bean);
		}

		if (opType.equals("add")) {
			// 保存员工
			String strBeginDate = DateUtil.dateToStr(this.bean.getBeginDate().getValue());
			uecOutStaffWork.setBeginDate(strBeginDate);
			String strEndDate = DateUtil.dateToStr(this.bean.getEndDate().getValue());
			uecOutStaffWork.setEndDate(strEndDate);
			String wbBeginDate = DateUtil.dateToStr(this.bean.getWbBeginDate().getValue());
			uecOutStaffWork.setWbBeginDate(wbBeginDate);
			String wbEndDate = DateUtil.dateToStr(this.bean.getWbEndDate().getValue());
			uecOutStaffWork.setWbEndDate(wbEndDate);
			uecOutStaffWork.setPsnStatus(0L);

			List<UecOutStaffWork> uecOutStaffWorksList = uecOutStaffManager.queryUecOutStaffWork(uecOutStaffInfo);
			for (int i = 0; i < uecOutStaffWorksList.size(); i++) {
				uecOutStaffWorksList.get(i).updateOnly();
			}
			uecOutStaffWork.addOnly();
			Events.postEvent("onOK", this.self, uecOutStaffWork);
			onCancel();
			Messagebox.show("新增成功！");
		} else if (opType.equals("edit")) {
			String strBeginDate = DateUtil.dateToStr(this.bean.getBeginDate().getValue());
			uecOutStaffWork.setBeginDate(strBeginDate);
			String strEndDate = DateUtil.dateToStr(this.bean.getEndDate().getValue());
			uecOutStaffWork.setEndDate(strEndDate);
			String wbBeginDate = DateUtil.dateToStr(this.bean.getWbBeginDate().getValue());
			uecOutStaffWork.setWbBeginDate(wbBeginDate);
			String wbEndDate = DateUtil.dateToStr(this.bean.getWbEndDate().getValue());
			uecOutStaffWork.setWbEndDate(wbEndDate);
			uecOutStaffWork.setPsnStatus(0L);
			uecOutStaffWork.setStatusCd("1000");
			uecOutStaffWork.updateOnly();
			Events.postEvent("onOK", this.self, uecOutStaffWork);
			onCancel();
			Messagebox.show("修改成功！");
		}

		onCancel();
	}

	/**
	 * 取消.
	 */
	public void onCancel() {
		bean.getUecOutStaffWorkEditWin().onClose();
	}

	private String checkUacStaffWorkData() {
		// BaseInfo Check
		if (StrUtil.isNullOrEmpty(bean.getJobName().getValue())) {
			return "请填写岗位名称";
		} else if (StrUtil.checkBlank(bean.getJobName().getValue())) {
			return "岗位名称中有空格";
		} else if (StrUtil.isNullOrEmpty(bean.getBeginDate().getValue())) {
			return "请填写在本单位从事外包开始日期";
		}
		// IDCARD Real Name Check
		/*
		 * if (EnumUacStaffInfo.CERT_TYPE_IDCARD.getValue().equals(
		 * bean.getCertType().getSelectedItem().getValue())) { String idNum =
		 * bean.getCertNumber().getValue(); String idName =
		 * bean.getCertName().getValue(); try { boolean isRealName =
		 * CertUtil.checkIdCard(idNum, idName);
		 * 
		 * if (!isRealName) { return "实名认证未通过"; } } catch (Exception e) { return
		 * "调用国政通接口失败"; }
		 * 
		 * }
		 */

		return null;
	}

	/**
	 * 字符串转date
	 */
	private void strToDate() {
		if (arg.get("uecOutStaffWork") != null) {
			uecOutStaffWork = (UecOutStaffWork) arg.get("uecOutStaffWork");
			PubUtil.fillBeanFromPo(uecOutStaffWork, bean);
			if (!StrUtil.isEmpty(uecOutStaffWork.getBeginDate())) {
				bean.getBeginDate().setValue(DateUtil.patternStrToDate(uecOutStaffWork.getBeginDate()));
			}
			if (!StrUtil.isEmpty(uecOutStaffWork.getEndDate())) {
				bean.getEndDate().setValue(DateUtil.patternStrToDate(uecOutStaffWork.getEndDate()));
			}
			if (!StrUtil.isEmpty(uecOutStaffWork.getWbBeginDate())) {
				bean.getWbBeginDate().setValue(DateUtil.patternStrToDate(uecOutStaffWork.getWbBeginDate()));
			}
			if (!StrUtil.isEmpty(uecOutStaffWork.getWbEndDate())) {
				bean.getWbEndDate().setValue(DateUtil.patternStrToDate(uecOutStaffWork.getWbEndDate()));
			}
		}
	}

	/**
	 * 绑定下拉框 绑定combobox
	 */
	private void bindCombobox() {
		// 人员状态
		List<NodeVo> nodeVoSexList = new ArrayList<NodeVo>();
		String[][] sex = { { "0", "在职" }, { "1", "退出" } };
		for (int i = 0; i < sex.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(sex[i][j]);
				nodeVo.setName(sex[i][j + 1]);
			}
			nodeVoSexList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getPsnStatus(), nodeVoSexList);
		// 人员类型
		List<NodeVo> nodeVoIsList = new ArrayList<NodeVo>();
		String[][] is = { { "0", "新进" }, { "1", "变更" }, { "3", "退出" } };
		for (int i = 0; i < is.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(is[i][j]);
				nodeVo.setName(is[i][j + 1]);
			}
			nodeVoIsList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getPsnType(), nodeVoIsList);
		// 是否小CEO
		List<NodeVo> nodeVoIsSendList = new ArrayList<NodeVo>();
		String[][] isSend = { { "1", "是" }, { "0", "否" } };
		for (int i = 0; i < isSend.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(isSend[i][j]);
				nodeVo.setName(isSend[i][j + 1]);
			}
			nodeVoIsSendList.add(nodeVo);
		}

		ListboxUtils.rendererForEdit(bean.getIsSCEO(), nodeVoIsSendList);
		// 外包公司用工类别
		ListboxUtils.rendererForEdit(bean.getJobType(), NodeVoUtil.getDictionaries(UecConstants.OUT_JOB_TYPE));
	}

	/**
	 * 将编码翻译展示
	 */
	public void codeTranslateDisplay() {
		if (uecOutStaffWork != null) {
			// 业务管理公司
			bean.getBusiCorp().setValue(uecOrgOrgsManager.getUecOrgOrgsNameByPkOrg(uecOutStaffWork.getBusiCorp()));
			// 业务管理部门
			bean.getBusiDept().setValue(uecOrgDeptManager.getUecOrgDeptNameByPkDept(uecOutStaffWork.getBusiDept()));
			// 基准岗位
			bean.getPkOmJob().setValue(uecPostManager.getUecPostNameByPkPost(uecOutStaffWork.getPkOmJob()));
		}
	}
}
